from django.conf import settings

import sys
import os


BASE_DIR = os.path.dirname(__file__)

settings.configure(
    DEBUG=True,
    SECRET_KEY='thisisthesecretkey',
    ROOT_URLCONF='sitebuilder.urls',
    NIDDLEWARE_CLASSES=(),
    INSTALLED_APPS=(
        'django.contrib.staticfiles',
        'sitebuilder',
    ),
    TEMPLATES=[
        {
            'BACKEND' : 'django.template.backends.django.DjangoTemplates',
            'DIRS' : [],
            'APP_DIRS': True,
        }
    ],
    STATICFILES_DIRS=[
        os.path.join(BASE_DIR,'static'),
    ],
    STATIC_URL='/static/',
    SITE_PAGES_DIRECTORY=os.path.join(BASE_DIR,'pages'),
    SITE_OUTPUT_DIRECTORY=os.path.join(BASE_DIR,'_build'),
    STATIC_ROOT=os.path.join(BASE_DIR,'_build','static'),
    STATICFILES_FINDERS=(
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',

    ),
)

if __name__=='__main__':
    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)


# class ImageForm(forms.Form):
#
#     height = forms.IntegerField(min_value=1,max_value=2000)
#     width = forms.IntegerField(min_value=1,max_value=2000)
#
#     def generate(self,image_format='PNG'):
#
#         height = self.cleaned_data['height']
#         width = self.cleaned_data['width']
#         key = '{}.{}.{}'.format(width,height,image_format)
#         content = cache.get(key)
#
#         if content is None:
#             image = Image.new('RGB',(width,height))
#             draw = ImageDraw.Draw(image)
#             text='{} X {}'.format(width,height)
#             textwidth,textheight = draw.textsize(text)
#             if textwidth < width and textheight < height:
#                 texttop = (height - textheight) // 2
#                 textleft = (width - textwidth) // 2
#                 draw.text((textleft,texttop), text,fill=(255,255,255))
#         content = BytesIO()
#         image.save(content,image_format)
#         content.seek(0)
#         cache.set(key,content,60 * 60)
#         return content
#
#
#
#
#
# def genrate_etag(request,width,height):
#     content = 'Placeholder:{0} x {1}'.format(width,height)
#     return hashlib.sha1(content.encode('utf-8')).hexdigest()
#
#
# @etag(genrate_etag)
# def placeholder(request,width,height):
#     form = ImageForm({'height': height, 'width': width})
#     if form.is_valid():
#         image = form.generate()
#         return HttpResponse(image, content_type='image/png')
#     else:
#         return  HttpResponseBadRequest('Invalid Image Request')
#
# def index(request):
#     example = reverse('placeholder',kwargs={'width' : 50, 'height' : 50})
#     context={
#         'example' : request.build_absolute_uri(example)
#     }
#     return render(request,'home.html',context)
#
#
# def dowload(request):
#
#     if request.method == 'GET':
#
#         file = open('static/write.xls', 'rb')
#         response = HttpResponse(file)
#         response['Content-Type'] = 'application/octet-stream'  # 设置头信息，告诉浏览器这是个文件
#         response['Content-Disposition'] = 'attachment;filename="write.xls"'
#         return response
#
#
#
# urlpatterns = (
#     url(r'^image/(?P<width>[0-9]+)x(?P<height>[0-9]+)/$',placeholder,name='placeholder'),
#     url(r'^$',index),
#     url(r'^dowload',dowload,name='dowload')
# )
#

